package lt.mii.optimum.components;

import java.util.Iterator;
import java.util.Map.Entry;

/**
 * Class Represents Single Student
 * 
 * @author dstrukov
 * 
 */
public class Student extends Person {

	private Group group;
	private String error = "";



	public Student() {

	}



	public boolean canStudy(Lesson lesson) {

		if ($lessons.contains(lesson)) {
			this.error = "already in the lesson";
			return false;

		}

		if (lesson.getFlow().getPersonList().contains(this)) {
			this.error = "already in the flow";
			return false;

		}
		if (!needsToStudy(lesson.getSubject())) {
			this.error = "does not need to study";
			return false;

		}
		return true;
	}



	public boolean needsToStudy(Subject subject) {

		if (!$hours.containsKey(subject)) {
			// System.out.println($hours.keySet() +" "+ subject.subjCode);
			return false;
		}

		if ($hours.get(subject) <= 0) {
			// System.out.println($hours.get(subject)+" "+ subject.subjCode);
			return false;
		}
		return true;
	}



	@Override
	public String toString() {
		String retStr = $name + " [";

		Iterator<Entry<Subject, Integer>> it = $hours.entrySet().iterator();
		while (it.hasNext()) {
			Entry<Subject, Integer> entry = it.next();
			retStr += "[ " + entry.getKey().getSubjCode() + " : "
					+ entry.getValue() + " ] ";
		}
		retStr += " ]";

		return retStr;
	}



	public Group getGroup() {
		return group;
	}



	public void setGroup(Group group) {
		this.group = group;
		group.addStudent(this);

	}
}
